package com.bl.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.bl.domain.Store;
import com.bl.service.IStoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/store")
@RefreshScope // 动态刷新配置注解
public class StoreController {
    @Autowired
    private IStoreService storeService;



    @PutMapping("/add")
    public String add(@RequestBody Store store){

        try {
            storeService.add(store);
            return "添加成功";
        } catch (Exception e) {
          e.printStackTrace();
          return "添加失败";
        }

    }

    @GetMapping("/getById/{id}")
    @SentinelResource(value="store-getById",  // Sentnel 限流规则名称
            blockHandler = "demotionMethod" // 指定降级方法
    )
    public Store getById(@PathVariable("id") Long id){
        return  storeService.getById(id);
    }

    // 降级方法
    public Store demotionMethod(@PathVariable("id") Long id, BlockException e){
        e.printStackTrace();
        return new Store(-1L,"请骚后再试，点击太快勒",-1L,null) ;
    }


}
